package com.kgc.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kgc.scd.bean.*;
import com.kgc.scd.mapper.OrderItemMapper;
import com.kgc.scd.mapper.OrderMapper;
import com.kgc.scd.mapper.ProductMapperJzp;
import com.kgc.scd.mapper.SeckillMapper;
import com.kgc.scd.service.PlaceOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created On : 2022/6/15.
 * <p>
 * Author     : 98730
 * <p>
 * Description: PlaceOrderServiceImpl
 */
@Slf4j
@Service
public class PlaceOrderServiceImpl implements PlaceOrderService {

    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Autowired(required = false)
    private ProductMapperJzp productMapperJzp;

    @Autowired(required = false)
    private OrderItemMapper orderItemMapper;

    @Autowired(required = false)
    private SeckillMapper seckillMapper;

    /**
     * @author : 98730
     * @date   : 2022/6/15
     * @param  : [com.kgc.scd.bean.Order]
     * @return : boolean
     * @description : 根据用户选择地址，优惠券，更新列表
     */
    @Override
    public boolean modOrderDetail(Order order) {
        return orderMapper.updateById(order) > 0;
    }

    /**
     * @author : 98730
     * @date   : 2022/6/16
     * @param  : [com.kgc.scd.bean.Product]
     * @return : boolean
     * @description : 更新商品库存
     */
    @Override
    public boolean modProductDetail(Product product) {
        return productMapperJzp.updateByPrimaryKeySelective(product) > 0;
    }

    @Override
    public List<OrderItem> queryOrderItemByOrderId(String orderId) {
        OrderItemExample orderItemExample = new OrderItemExample();
        OrderItemExample.Criteria criteria = orderItemExample.createCriteria();
        criteria.andItemOrderidEqualTo(orderId);
        return orderItemMapper.selectByExample(orderItemExample);
    }

    @Override
    public Product queryProductByProId(String proId) {
        return productMapperJzp.selectByPrimaryKey(proId);
    }

    @Override
    public Seckill querySeckillProductByProId(String proId) {
        log.info(proId + "********");
        SeckillExample seckillExample = new SeckillExample();
        SeckillExample.Criteria criteria = seckillExample.createCriteria();
        criteria.andSeckillProidEqualTo(proId);
        List<Seckill> seckills = seckillMapper.selectByExample(seckillExample);
        log.info("seckills:{}",seckills);
        if (seckills != null && !seckills.isEmpty()) {
            return seckills.get(0);
        }
        return null;
    }

    @Override
    public boolean modSeckillDetail(Seckill seckill) {
        //SeckillExample seckillExample = new SeckillExample();
        //SeckillExample.Criteria criteria = seckillExample.createCriteria();
        //criteria.andSeckillProidEqualTo(seckill.getSeckillProid());
        //return seckillMapper.updateByExampleSelective(seckill,seckillExample) > 0;
        UpdateWrapper<Seckill> seckillUpdateWrapper = new UpdateWrapper<>();
        seckillUpdateWrapper.eq("seckill_proid",seckill.getSeckillProid());
        return  seckillMapper.update(seckill, seckillUpdateWrapper) > 0;
    }

    @Override
    public Order queryOrder(String orderId) {
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("order_id",orderId);
        Order order = orderMapper.selectOne(orderQueryWrapper);
        return  order;


        //OrderExample orderExample = new OrderExample();
        //OrderExample.Criteria criteria = orderExample.createCriteria();
        //criteria.andOrderIdEqualTo(orderId);
        //if (orderMapper.selectByExample(orderExample) != null){
        //    return orderMapper.selectByExample(orderExample).get(0);
        //}
        //return null;
    }
}
